cmake_minimum_required(VERSION 2.8)
project(x2)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -g -O0 -std=c++11 -fopenmp")

# Get the current working branch
execute_process(
  COMMAND git rev-parse --abbrev-ref HEAD
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE GIT_BRANCH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Get the latest abbreviated commit hash of the working branch
execute_process(
  COMMAND git log -1 --format=%h
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE GIT_COMMIT_HASH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

configure_file(
  version.h.in
  ${CMAKE_BINARY_DIR}/version.h
)
set(CMAKE_MODULE_PATH ${x2_SOURCE_DIR}/cmake)

message(STATUS ${CMAKE_INSTALL_PREFIX})
set(CMAKE_INSTALL_RPATH $ORIGIN/../lib)

########################################################################
find_package(PkgConfig REQUIRED)

#----------------------------------------------------------------------#
find_package(Protobuf)
if (NOT PROTOBUF_FOUND)
    message(FATAL_ERROR "PROTOBUF NOT FOUND")
endif()
include_directories(${PROTOBUF_INCLUDE_DIRS})

#----------------------------------------------------------------------#
find_package(libevhtp)
if (NOT LIBEVHTP_FOUND)
    message(FATAL_ERROR "libevhtp NOT FOUND")
endif()
include_directories(${LIBEVHTP_INCLUDE_DIRS})

#----------------------------------------------------------------------#
find_package(lcb)
if (NOT lcb_FOUND)
    message(FATAL_ERROR "lcb NOT FOUND")
endif()
include_directories(${lcb_INCLUDE_DIRS})

#----------------------------------------------------------------------#
pkg_check_modules(TCMALLOC libtcmalloc)
if (NOT TCMALLOC_FOUND)
    message(FATAL_ERROR "TCMALLOC NOT FOUND")
endif()
include_directories(${TCMALLOC_INCLUDE_DIRS})

#----------------------------------------------------------------------#
pkg_check_modules(LIBSSL libssl)
if (NOT LIBSSL_FOUND)
    message(FATAL_ERROR "LIBSSL NOT FOUND")
endif()
include_directories(${LIBSSL_INCLUDE_DIRS})

#----------------------------------------------------------------------#
pkg_check_modules(PROF libprofiler)
if (NOT PROF_FOUND)
    message(FATAL_ERROR "PROF NOT FOUND")
endif()
include_directories(${PROF_INCLUDE_DIRS})

#----------------------------------------------------------------------#
pkg_check_modules(LIBEVENT libevent)
if (NOT LIBEVENT_FOUND)
    message(FATAL_ERROR "LIBEVENT NOT FOUND")
endif()
include_directories(${LIBEVENT_INCLUDE_DIRS})

#----------------------------------------------------------------------#
pkg_check_modules(LIBCURL libcurl)
if (NOT LIBCURL_FOUND)
    message(FATAL_ERROR "LIBCURL NOT FOUND")
endif()
include_directories(${LIBCURL_INCLUDE_DIRS})

#----------------------------------------------------------------------#
pkg_check_modules(LIBUV libuv)
if (NOT LIBUV_FOUND)
    message(FATAL_ERROR "LIBUV NOT FOUND")
endif()
include_directories(${LIBUV_INCLUDE_DIRS})

#----------------------------------------------------------------------#
pkg_check_modules(ZLIB zlib)
if (NOT ZLIB_FOUND)
    message(FATAL_ERROR "ZLIB NOT FOUND")
endif()
include_directories(${ZLIB_INCLUDE_DIRS})

########################################################################
include_directories(.)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE -std=c99 -fno-omit-frame-pointer -g -O0 -fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GNU_SOURCE -std=c++11 -fno-omit-frame-pointer -g -O0 -fopenmp")

include_directories(${CMAKE_BINARY_DIR})
include_directories(.)

add_subdirectory(share tmp/share)
add_subdirectory(logon tmp/logon)
add_subdirectory(server tmp/server)
add_subdirectory(auth tmp/auth)
add_subdirectory(bill tmp/bill)
add_subdirectory(billclient tmp/billclient)
add_subdirectory(robot tmp/robot)
add_subdirectory(test tmp/test)
add_subdirectory(gencode tmp/gencode)
